package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.service.FeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "globalFallbackMethod")
public class OrderController {

    @Resource
    private FeignService feignService;

    @GetMapping("/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id) {
        return feignService.getPaymentInfo_OK(id);
    }

    @GetMapping("/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "getPaymentInfo_TimeoutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
//    })
    public String getPaymentInfo_Timeout(@PathVariable("id") Integer id) {
        return feignService.getPaymentInfo_Timeout(id);
    }

    @GetMapping("/hystrix/global/{id}")
//    @HystrixCommand
    public String getPaymentInfoGlobalFallback(@PathVariable("id")Integer id) {
        return feignService.getPaymentInfo_Timeout(id);
    }

    @GetMapping("/hystrix/{id}")
    public String getInfoByTimeout(@PathVariable("id") Integer id) {
        return feignService.getPaymentInfo_Timeout(id);
    }

    // 此处fallback方法必须跟调用方法参数列表相同，否则会报找不到该方法
    public String getPaymentInfo_TimeoutHandler(Integer id) {
        return "客户端调用超时，服务器正忙，请稍后重试";
    }


    // 下面是全局fallbackMethod
    public String globalFallbackMethod() {
        return "全局通用降级，系统异常，请稍后再试";
    }

}
